package com.qlm.config;

import java.awt.BasicStroke;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.imageio.ImageIO;

import com.google.zxing.common.BitMatrix;


/**
 * 生成和解析相关工具类代码
 * @author yancy
 *
 */
public final class MatrixToImageWriter {
	   
	   private MatrixToImageWriter() {}  
	   
	     
	   public static BufferedImage toBufferedImage(BitMatrix matrix,int color,int bgcolor) {  
	     int width = matrix.getWidth();  
	     int height = matrix.getHeight();  
	     BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);  
	     for (int x = 0; x < width; x++) {  
	       for (int y = 0; y < height; y++) {  
	         image.setRGB(x, y, matrix.get(x, y) ? color : bgcolor);  
	       }  
	     }  
	     return image;  
	   }  
	   
	   /**
	    * 将图片写入文件
	    * @param matrix
	    * @param format
	    * @param file
	    * @param logoImg
	    * @param color
	    * @param bgcolor
	    * @param logoConfig
	    * @throws IOException
	    */
	   public static void writeToFile(BitMatrix matrix, String format, File file,int color,int bgcolor,InputStream is,LogoConfig logoConfig)  
	       throws IOException {  
	     BufferedImage image = toBufferedImage(matrix,color,bgcolor);  
	     if(is!=null){
				Graphics2D g = image.createGraphics();
				/**
				 * 读取Logo图片
				 */
//				BufferedImage logo = ImageIO.read(new FileInputStream(logoPath) );
				BufferedImage logo = ImageIO.read(is);
				/**
				 * 设置logo的大小,这里设置为二维码图片的20%,因为过大会盖掉二维码
				 */
				int widthLogo = logo.getWidth(null) > image.getWidth() * 2 / 10 ? (image
						.getWidth() * 2 / 10) : logo.getWidth(null), heightLogo = logo
						.getHeight(null) > image.getHeight() * 2 / 10 ? (image
						.getHeight() * 2 / 10) : logo.getWidth(null);

				// 计算图片放置位置
				/**
				 * logo放在中心
				 */
				 int x = (image.getWidth() - widthLogo) / 2;
				 int y = (image.getHeight() - heightLogo) / 2;
				/**
				 * logo放在右下角
				 */
//				int x = (image.getWidth() - widthLogo);
//				int y = (image.getHeight() - heightLogo);
				 
				// 开始绘制图片
				g.drawImage(logo, x, y, widthLogo, heightLogo, null);
				g.drawRoundRect(x, y, widthLogo, heightLogo, 15, 15);
				g.setStroke(new BasicStroke(logoConfig.getBorder()));
				g.setColor(logoConfig.getBorderColor());
				g.drawRect(x, y, widthLogo, heightLogo);

				g.dispose();
				logo.flush();
				image.flush();
	     }
	     if (!ImageIO.write(image, format, file)) {  
	       throw new IOException("Could not write an image of format " + format + " to " + file);  
	     }  
	   }  
	   
	     
	   public static void writeToStream(BitMatrix matrix, String format, OutputStream stream,int color,int bgcolor)  
	       throws IOException {  
	     BufferedImage image = toBufferedImage(matrix,color,bgcolor);  
	     if (!ImageIO.write(image, format, stream)) {  
	       throw new IOException("Could not write an image of format " + format);  
	     }  
	   } 
}
